// index.js
import { formatNumber } from '../../utils/util'
import request from '../../utils/request'
const app = getApp()

Page({
  data: {
    tickets: []
  },
  onLoad() {
    const token = app.globalData.token
    if (!token) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        showCancel: false,
        success: res => {
          wx.redirectTo({
            url: '/pages/login/index'
          })
        }
      })
      return
    }
  },
  async onShow() {
    if (!app.globalData.token) return
    //暂时获取100条数据，后面改为获取全部数据 或 触底加载新数据
    const data = await request.get('/mvs/ticket/list', { page: 1, limit: 100 })
    this.setData({
      tickets: data.page.list.map(item => ({
        ...item,
        waitFine: formatNumber(item.waitFine) + ' 元',
        waitOverdueFee: formatNumber(item.waitOverdueFee) + ' 元'
      }))
    })
  },
  clickHanle(e) {
    wx.navigateTo({
      url: `/pages/ticket/index?id=${e.currentTarget.dataset.id}`
    })
  }
})
